<?php

/**
 * client form base class.
 *
 * @method client getObject() Returns the current form's model object
 *
 * @package    e-DSL
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseclientForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'datecreation'          => new sfWidgetFormDate(),
      'code'                  => new sfWidgetFormInputText(),
      'categorieclient_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('categorieclient'), 'add_empty' => false)),
      'nom'                   => new sfWidgetFormInputText(),
      'forme_juridique_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('forme_juridique'), 'add_empty' => true)),
      'commercial_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('commercial'), 'add_empty' => false)),
      'adresse'               => new sfWidgetFormTextarea(),
      'codpos'                => new sfWidgetFormInputText(),
      'ville'                 => new sfWidgetFormInputText(),
      'departement_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('departement'), 'add_empty' => true)),
      'telephone'             => new sfWidgetFormInputText(),
      'fax'                   => new sfWidgetFormInputText(),
      'email'                 => new sfWidgetFormInputText(),
      'web'                   => new sfWidgetFormInputText(),
      'activite_id'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('activite'), 'add_empty' => true)),
      'reseau_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('reseau'), 'add_empty' => true)),
      'siret'                 => new sfWidgetFormInputText(),
      'encour'                => new sfWidgetFormInputText(),
      'delais_paiement_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('delais_paiement'), 'add_empty' => true)),
      'dirigeant'             => new sfWidgetFormInputText(),
      'fonction_dirigeant_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('fonction_dirigeant'), 'add_empty' => true)),
      'contactes_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('contactes'), 'add_empty' => true)),
      'remises_id'            => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('remises'), 'add_empty' => true)),
      'codes_id'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('codes'), 'add_empty' => true)),
      'concurrents_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('concurrents'), 'add_empty' => true)),
      'observations'          => new sfWidgetFormTextarea(),
      'longitude'             => new sfWidgetFormInputText(),
      'latitude'              => new sfWidgetFormInputText(),
      'location_type'         => new sfWidgetFormInputText(),
      'address_type'          => new sfWidgetFormInputText(),
      'external_id'           => new sfWidgetFormInputText(),
      'activeStatus'          => new sfWidgetFormInputText(),
      'created_at'            => new sfWidgetFormDateTime(),
      'updated_at'            => new sfWidgetFormDateTime(),
      'fournisseurs_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'fournisseur')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'datecreation'          => new sfValidatorDate(array('required' => false)),
      'code'                  => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'categorieclient_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('categorieclient'))),
      'nom'                   => new sfValidatorString(array('max_length' => 255)),
      'forme_juridique_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('forme_juridique'), 'required' => false)),
      'commercial_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('commercial'))),
      'adresse'               => new sfValidatorString(array('max_length' => 300, 'required' => false)),
      'codpos'                => new sfValidatorString(array('max_length' => 5, 'required' => false)),
      'ville'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'departement_id'        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('departement'), 'required' => false)),
      'telephone'             => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'fax'                   => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'email'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'web'                   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'activite_id'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('activite'), 'required' => false)),
      'reseau_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('reseau'), 'required' => false)),
      'siret'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'encour'                => new sfValidatorNumber(array('required' => false)),
      'delais_paiement_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('delais_paiement'), 'required' => false)),
      'dirigeant'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'fonction_dirigeant_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('fonction_dirigeant'), 'required' => false)),
      'contactes_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('contactes'), 'required' => false)),
      'remises_id'            => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('remises'), 'required' => false)),
      'codes_id'              => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('codes'), 'required' => false)),
      'concurrents_id'        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('concurrents'), 'required' => false)),
      'observations'          => new sfValidatorString(array('required' => false)),
      'longitude'             => new sfValidatorNumber(array('required' => false)),
      'latitude'              => new sfValidatorNumber(array('required' => false)),
      'location_type'         => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'address_type'          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'external_id'           => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'activeStatus'          => new sfValidatorInteger(),
      'created_at'            => new sfValidatorDateTime(),
      'updated_at'            => new sfValidatorDateTime(),
      'fournisseurs_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'fournisseur', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('client[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'client';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['fournisseurs_list']))
    {
      $this->setDefault('fournisseurs_list', $this->object->fournisseurs->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savefournisseursList($con);

    parent::doSave($con);
  }

  public function savefournisseursList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['fournisseurs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->fournisseurs->getPrimaryKeys();
    $values = $this->getValue('fournisseurs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('fournisseurs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('fournisseurs', array_values($link));
    }
  }

}
